package com.longshine.plc.ui;

import com.longshine.plc.data.DataSource;
import com.longshine.plc.net.ServerService;
import com.longshine.util.NumberUtil;
import java.awt.Container;
import java.awt.EventQueue;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;
import javax.swing.UnsupportedLookAndFeelException;
import org.jdesktop.layout.GroupLayout;
import org.jdesktop.layout.GroupLayout.ParallelGroup;
import org.jdesktop.layout.GroupLayout.SequentialGroup;

public class PLCMonitorDialog extends JDialog
  implements DataSource
{
  private List<OwnPanel> panelList;
  private ServerService server;
  private JButton jButton1;
  private JLabel jLabel1;
  private JTextField jTextField1;
  private JTabbedPane tabPanel;

  public void init()
  {
    setTitle("PLC模拟器");
    this.panelList = new ArrayList();
    this.panelList.add(new ProtocolPanel());
    this.panelList.add(new BerthStationSwitchPanel());
    this.panelList.add(new EquipPowerPanel());
    this.panelList.add(new AirCompressPanel());
    this.panelList.add(new BerthControlPanel());
    this.panelList.add(new ClientPanel());
    for (OwnPanel p : this.panelList) {
      p.init();
      this.tabPanel.add(p.getTitle(), p);
    }
    ActionListener l = new ActionListener()
    {
      public void actionPerformed(ActionEvent e) {
        String comm = e.getActionCommand();
        if ((e.getSource() == PLCMonitorDialog.this.jButton1) && (comm.equals("启动"))) {
          PLCMonitorDialog.this.serverStart();
        }
        else if ((e.getSource() == PLCMonitorDialog.this.jButton1) && (comm.equals("停止")))
          PLCMonitorDialog.this.serverStop();
      }
    };
    this.jButton1.addActionListener(l);
  }
  public void recv(String str) {
  }

  public void send(String str) {
  }

  public void serverStart() {
    try {
      int port = Integer.parseInt(this.jTextField1.getText());
      this.server = new ServerService(this);
      StartServerThread thread = new StartServerThread(this.server, port);
      thread.start();
      if (thread.isStartSuc())
        this.jButton1.setText("停止");
      else
        JOptionPane.showMessageDialog(this, "启动失败");
    }
    catch (Exception ex)
    {
      JOptionPane.showMessageDialog(this, "启动失败");
    }
  }

  public void serverStop() {
    this.server.stopServer();
    this.server = null;
    this.jButton1.setText("启动");
  }
  public String getBerthStationSwitchValue() {
    for (OwnPanel op : this.panelList) {
      if ((op instanceof BerthStationSwitchPanel)) {
        return ((BerthStationSwitchPanel)op).getHexData();
      }
    }
    return null;
  }

  public String getEquipPowerValue() {
    for (OwnPanel op : this.panelList) {
      if ((op instanceof EquipPowerPanel)) {
        return ((EquipPowerPanel)op).getHexData();
      }
    }
    return null;
  }

  public String getAirCompressValue() {
    for (OwnPanel op : this.panelList) {
      if ((op instanceof AirCompressPanel)) {
        return NumberUtil.toHexStr(((AirCompressPanel)op).getPressValue());
      }
    }
    return null;
  }
  public void log(String str) {
    for (OwnPanel op : this.panelList)
      if ((op instanceof ProtocolPanel)) {
        ((ProtocolPanel)op).log(str);
        return;
      }
  }

  public void setBerthControlState(String data) {
    for (OwnPanel op : this.panelList)
      if ((op instanceof BerthControlPanel)) {
        ((BerthControlPanel)op).setHexData(data);
        return;
      }
  }

  public PLCMonitorDialog(Frame parent, boolean modal)
  {
    super(parent, modal);
    initComponents();
    init();
  }

  private void initComponents()
  {
    this.jLabel1 = new JLabel();
    this.jButton1 = new JButton();
    this.tabPanel = new JTabbedPane();
    this.jTextField1 = new JTextField();

    setDefaultCloseOperation(2);

    this.jLabel1.setText("端口：");

    this.jButton1.setText("启动");

    GroupLayout layout = new GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
      layout.createParallelGroup(1)
      .add(layout.createSequentialGroup()
      .addContainerGap()
      .add(this.jLabel1)
      .add(44, 44, 44)
      .add(this.jTextField1, -2, 175, -2)
      .add(50, 50, 50)
      .add(this.jButton1)
      .addContainerGap(218, 32767))
      .add(2, this.tabPanel));

    layout.setVerticalGroup(
      layout.createParallelGroup(1)
      .add(layout.createSequentialGroup()
      .add(31, 31, 31)
      .add(layout.createParallelGroup(3)
      .add(this.jLabel1)
      .add(this.jTextField1, -2, -1, -2)
      .add(this.jButton1))
      .add(18, 18, 18)
      .add(this.tabPanel, -1, 586, 32767)));

    pack();
  }

  public static void main(String[] args)
  {
    try
    {
      for (UIManager.LookAndFeelInfo info : UIManager.getInstalledLookAndFeels())
        if ("Nimbus".equals(info.getName())) {
          UIManager.setLookAndFeel(info.getClassName());
          break;
        }
    }
    catch (ClassNotFoundException ex) {
      Logger.getLogger(PLCMonitorDialog.class.getName()).log(Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
      Logger.getLogger(PLCMonitorDialog.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
      Logger.getLogger(PLCMonitorDialog.class.getName()).log(Level.SEVERE, null, ex);
    } catch (UnsupportedLookAndFeelException ex) {
      Logger.getLogger(PLCMonitorDialog.class.getName()).log(Level.SEVERE, null, ex);
    }

    EventQueue.invokeLater(new Runnable()
    {
      public void run() {
        PLCMonitorDialog dialog = new PLCMonitorDialog(new JFrame(), true);
        dialog.addWindowListener(new WindowAdapter()
        {
          public void windowClosing(WindowEvent e)
          {
            System.exit(0);
          }
        });
        dialog.setVisible(true);
      }
    });
  }
}